package saucaca.fragment;

import java.util.ArrayList;

import org.json.JSONException;

import saucaca.Adapter.CustomAdapterVideoListview;
import saucaca.Adapter.TvshowAdapter;
import saucaca.CustomView.ProgressBarCircularIndeterminate;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.SongObject;
import saucaca.Object.TvshowObject;
import saucaca.fragment.TvshowDetails.GetFromServices;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

public class TvShowFragment2 extends Fragment implements OnScrollListener {
	private MainActivity mActivity = null;
	String cateCode = "";
	String singerCode = "";
	String authorCode = "";
	String areaCode = "";
	String typeHot = "1";
	String page = "0";
	String pageSize = "";
	int xpage = 0;
	private ListView listView;
	boolean loadingMore;
	private View mView_EmptyLoading = null;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	ArrayList<TvshowObject> listTvshow;
	TextView txtStatus;

	TvshowAdapter adapter;
	ProgressBarCircularIndeterminate progressBarFooter;
	int m_PreviousTotalCount = 0;

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.listview2, container, false);
		findviewByID(view);
		new GetFromServices().execute("");

		mActivity.myActionBar.setMenu(R.string.menu_tvshow, false, true);
		return view;
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if (!hidden)
			mActivity.myActionBar.setMenu(R.string.menu_tvshow, false, true);
	}

	private void findviewByID(View view) {
		listView = (ListView) view.findViewById(R.id.layoutlistview_listview);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		listView.setEmptyView(mView_EmptyLoading);
		listView.setOnScrollListener(this);
		progressBar = (ProgressBar) mView_EmptyLoading.findViewById(R.id.empty_loading_progress);
		imgEmpty = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_NoItem);
		imgError = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_Error);
		txtStatus = (TextView) mView_EmptyLoading.findViewById(R.id.empty_loading_text);

		progressBarFooter = (ProgressBarCircularIndeterminate) view.findViewById(R.id.progressBarloadmore);
		listTvshow = new ArrayList<TvshowObject>();
		adapter = new TvshowAdapter(mActivity, listTvshow);
		listView.setAdapter(adapter);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}

	class GetFromServices extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... param) {
			if (m_PreviousTotalCount > 0)
				getActivity().runOnUiThread(new Runnable() {
					@Override
					public void run() {
						progressBarFooter.setVisibility(View.VISIBLE);
					}
				});
			String str = WServiceLib.tvshowList(page, pageSize, areaCode);
			// BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null)
				try {
					if(getActivity()!=null)
						getActivity().runOnUiThread(new Runnable() {
							@Override
							public void run() {
								progressBarFooter.setVisibility(View.GONE);
							}
						});
					// listTvshow = JsonReader.readTvshow(result);
					ArrayList<TvshowObject> listShows = JsonReader.readTvshow(result);
					if (listShows.size() > 0)
						for (int i = 0; i < listShows.size(); i++) {
							boolean isAdded = false;
							for (int j = 0; j < listTvshow.size(); j++) {
								if (listTvshow.get(j).getCATE_ID().equals(listShows.get(i).getCATE_ID()))
									isAdded = true;
							}
							if (!isAdded) {
								listTvshow.add(listShows.get(i));
								adapter.update(listTvshow);
							}
						}
					if (listTvshow.size() == 0) {
						progressBar.setVisibility(View.GONE);
						imgEmpty.setVisibility(View.VISIBLE);
						txtStatus.setText(mActivity.getResources().getString(R.string.no_item_song));
					}
					loadingMore = false;
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (totalItemCount == 0 || adapter == null)
			return;
		if (m_PreviousTotalCount == totalItemCount) {
			return;
		}
		boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount - 3;
		if (loadMore && firstVisibleItem > 0 && totalItemCount - m_PreviousTotalCount >= 10) {
			m_PreviousTotalCount = totalItemCount;
			xpage += 1;
			new GetFromServices().execute("");
		}

	}
}
